<?php 
namespace Teacher\Controller;

use Admin\Controller\AdminbaseController;
class SignadminController extends AdminbaseController{
	
	protected $m;
	protected $udr;
	protected $lst;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclSign');
		$this->udr = D('UsersDistrictRelation');
		$this->lst = D("LessonStudentRelation");
	}

	
	public function index(){
		$this->display();
	}
	
	
	public function edit(){
		if(isset($_GET['id'])){
			$info = $this->m->find($_GET['id']);		
			$this->info = $info;
			
			$lid = $info['lid'];
		}else{
			$lid = I('lid');
		}
		//当前学期
		$terminfo = F('Termdata');
		//获取学生
// 		$udrid = I('udrid');
		$this->lid = $lid;
		$students = $this->lst->where(array('tid'=>$terminfo['id'],'lid'=>$lid))->select();
		$this->students = $students;
		$this->ids = implode(',', array_column($students,'id'));
		$this->display();
	}
	
	public function edit_post(){
		if(IS_POST){
			$terminfo = F('Termdata');
			$_POST['tid'] = $terminfo['id'];
			
			$users = $_POST['users'];
			foreach ($users as $key=>$us){
				if($us == 1) $dao[] = $key;
				if($us == 2) $weidao[] = $key;
				if($us == 3) $qingjia[] = $key;
			}
			if($dao){
				$_POST['dao'] = implode(',', $dao);
				$_POST['daoname'] = $this->getstuname($_POST['dao']);
			}else{
				$_POST['dao'] = '';
				$_POST['daoname'] = '';
			}
			if($weidao){
				$_POST['weidao'] = implode(',', $weidao);
				$_POST['weidaoname'] = $this->getstuname($_POST['weidao']);
			}else{
				$_POST['weidao'] = '';
				$_POST['weidaoname'] = '';
			}
			if($qingjia){
				$_POST['qingjia'] = implode(',', $qingjia);
				$_POST['qingjianame'] = $this->getstuname($_POST['qingjia']);
			}else{
				$_POST['qingjia'] = '';
				$_POST['qingjianame'] = '';
			}
			$_POST['update_time'] = Date('Y-m-d H:i:s');
			unset($_POST['users']);
			unset($_POST['ids']);
			
			if($_POST['id']){
				if($this->m->create()){
					if($this->m->save()){
						$this->success('更新成功');
					}else{
						$this->error('更新失败');
					}
				}
			}else{
				$_POST['create_time'] = Date('Y-m-d H:i:s');
				if($this->m->create()){
					$id = $this->m->add();
					if($id){
						$this->success('添加成功');
					}else{
						$this->error('添加失败');
					}
				}
			}
		}
	}
	
	public function del(){
		$id = array_unique((array)I('ids',0));
		if ( empty($id) ) {
			$this->error('请选择要操作的数据!');
		}
		$map = array('id' => array('in', $id));
		if($this->m->where($map)->delete()){
			$this->success('删除成功');
		} else {
			$this->error('删除失败！');
		}
	}
	
	public function listorders() {
		$status = parent::_listorders($this->m);
		if ($status) {
			$this->success("排序更新成功！",U('index'));
		} else {
			$this->error("排序更新失败！");
		}
	}
	
	public function changestatus(){
		$data[$this->m->getPk()] = $_REQUEST['id'];
		$data['status'] = $_REQUEST['status'];
		$data['update_time'] = date('Y-m-d H:i:s');
		if($this->m->save($data)){
			$this->success('状态更新成功');
		}else{
			$this->error('状态更新失败');
		}
	}
	
	public function getstuname($lsrids){
		$students = $this->lst->where(array('id'=>array('in',$lsrids)))->getField('student',true);
		return implode(',',$students);
	}
}

?>